<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  comment_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-17.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Comment_model extends CI_model{
	function __construct(){
		parent::__construct();
		// 安全模块
 		$this->load->model('common/security_model');
		// 动态模块
		$this->load->model('feed_model');
		// 我说模块
		$this->load->model('talk_model');
		// 作品模块
		$this->load->model('work_model');
		//提醒模块
		$this->load->model('notification_model');

	}
	
	function get_comments($id, $idtype, $offset= '0', $perpage= '50'){
		
		if ($id && $idtype) {
			$data = array();
			
			$wheresql = " id='".$id."' AND idtype ='".$idtype."' ";
			$orderbysql = " ORDER BY dateline DESC ";
			$data['count'] = $this->db->query("SELECT count(*) AS num FROM ".$this->db->dbprefix('comment')." WHERE $wheresql $orderbysql")->first_row()->num;
			if ($data['count']) {
				$sql = "SELECT * FROM ".$this->db->dbprefix('comment')." WHERE $wheresql $orderbysql LIMIT $offset, $perpage";
				$query = $this->db->query($sql);
				foreach ($query->result_array() as $row) {
					$row['avatar_small'] = avatar($row['uid'], 'small');
					$data['list'][] = $row;
				}
				return $data;
			}

		}
		return false;
	}
	
	function get_page_comments($id, $idtype){
		
		if ($id && $idtype) {
			switch ($idtype) {
				case 'workid':
					$mpurl = base_url().'work/index/'.$id;
					break;
				case 'talkid':
					$mpurl = base_url().'talk/detail/?uid='.$this->user['uid'].'&talkid='.$id;
					break;
				default:
					$mpurl = '';
					break;
			}
			
			//每页查看数
			$perpage = empty($_GET['perpage'])?0:intval($_GET['perpage']);
			if(!in_array($perpage, array(20,50,100))) $perpage = 50;

			//分页
			$multipage_config = $this->config->item('multipage_config', 'SGLOBAL');
			$multipage_config['base_url'] = $mpurl;
			$multipage_config['page_query_string'] = TRUE;
			$offset = $this->input->get('per_page') ? $this->input->get('per_page', true) : 0;

			$data = $this->get_comments($id, $idtype, $offset, $perpage);
			if($data['list']) {

				$multipage_config['per_page'] = $perpage;
				$multipage_config['total_rows'] = $data['count'];
				$this->pagination->initialize($multipage_config);
				$multi = $this->pagination->create_links();
				$data['multi'] = $multi;
			}
			return $data;
		}
		return false;
	}

	function get_comment_by_cid($cid){
		$comment = array();
		if ($cid) {
			$this->db->where('cid', $cid);
			$query = $this->db->get('comment');
			$comment = $query->row_array();
			return $comment;
		}
		return false;
	}
		
	function insert_comment($comments){
		if ($comments) {
			$this->db->insert('comment', $comments);
			return $this->db->insert_id();
		}else{
			return false;
		}
	}
	
	function delete_comment($cid){
		if ($cid) {
			$this->db->where('cid', $cid);
			$this->db->delete('comment');
			return $this->db->affected_rows();;
		}else{
			return false;
		}
	}
	
	
	function add_comment($id, $idtype, $message){
		if($id && $idtype && $message){
			$setarr = array();
			$originuid = '';
			//检查权限
			switch ($idtype) {
				case 'talkid':
					$talk = $this->talk_model->get_talk('talkid', $id);
					if (!$talk) {
						return false;
					}
					$originuid = $talk['uid'];
					
					//关注提醒
					$note = $this->lang->line('note_comment_talk')."<a href='".base_url().'talk/detail/?uid='.$talk['uid'].'&talkid='.$talk['talkid']."'>".cutstr($talk['message'], 75)."</a>";
					$this->notification_model->notification_add($talk['uid'], 'talkcomment', $note);
					
					break;
				case 'workid':
					$work = $this->work_model->get_work('wid', $id);
					if (!$work) {
						return false;
					}
					$originuid = $work['uid'];
					//关注提醒
					$note = $this->lang->line('note_comment_work')."<a href='".base_url().'work/index/'.$work['wid']."'>".cutstr($work['title'], 75)."</a>";
					$this->notification_model->notification_add($work['uid'], 'workcomment', $note);
					
					break;
				default:
					return false;
				break;
			}
			
			$comments = array(
					'uid' => $originuid,
					'id' => $id,
					'idtype' => $idtype,
					'authorid' => $this->user['uid'],
					'author' => $this->user['name'],
					'dateline' => now(),
					'message' => $message,
					'ip' => getonlineip()
				);
			$insert_id = $this->insert_comment($comments);
			// 评论数
			if ($insert_id) {
				switch ($idtype) {
					case 'talkid':
						//增加我说评论数
						$this->db->query("UPDATE ".$this->db->dbprefix('talk')." SET replynum = replynum + 1 WHERE talkid='".$id."'");
						break;
					case 'workid':
						$this->db->query("UPDATE ".$this->db->dbprefix('works')." SET replynum = replynum + 1 WHERE wid='".$id."'");
						break;
					break;
				}
				//增加feed评论数
				$this->db->query("UPDATE ".$this->db->dbprefix('feed')." SET replynum = replynum + 1 WHERE id='".$id."' AND idtype='".$idtype."'");
				// 更新session
				$this->security_model->update_user_session();

			}
			return 1;
		}
		return false;
	}
}
/* End of file welcome.php */
/* Location: ./application/controllers/index.php */
?>